﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.ComponentModel;
using System.IO;
using AlexandriaPressPublisherBusinessLayer;

namespace AlexandriaPressWebsite.MasterPages
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void downloadButton_Click(object sender, EventArgs e)
        {
            int numHits = 0;
            string destinationUrl = "";
     
            //sourceUrl = @"C:\Documents and Settings\User\Desktop\AlexandriaPressWebsite\AlexandriaPressWebsite\Drafts\" + filename;                              
            WebClient client = new WebClient();

            client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);

            try
            {
                client.DownloadFileAsync(new Uri(ProofreaderProcessManager.downloadDraft(Convert.ToInt32(Session["draftForDownload"]), 1/*Convert.ToInt32(Session["userID"])*/, ref numHits, ref destinationUrl)), destinationUrl);
            }
            catch (Exception ex)
            {
                downloadMessageLabel.Text = "Download failed";
            }
   

            if (numHits > 0)
            {
                downloadMessageLabel.Text = "Unfortunately, the draft was assigned to someone else. The operation was cancelled.";
                downloadLabel.Visible = false;
                downloadButton.Visible = false;
                continueButton.Visible = true;
            }

  
        }

        public void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            downloadLabel.Visible = false;
            downloadButton.Visible = false;
            downloadMessageLabel.Text = "Download completed! Get the file in your Documents folder";
            continueButton.Visible = true;
        }

        protected void continueButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/MasterPages/ProofReaderConsole.aspx");
        }
    }
}
